import pylab as pl
from itertools import izip_longest

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip_longest(*[iter(iterable)]*n, fillvalue=padvalue)

while True:
	captura = open("c:\\users\\santiago\\Documents\\captura.txt",'r')
	lineas = captura.readlines()
	cadena = []
	for i in lineas:
		if i.split(' ')[0] == "space":
			cadena.append([0,0])
		elif i.split(' ')[0] == "pulse":
			cadena.append([0,1])
		cadena[-1][0] = 2 if int(i.split(' ')[1].replace('\n','')) > 800 else 1
	cadena.append([2,1])


	x = [a[0] for a in cadena]
	y = [a[1] for a in cadena]	
	c = []

	comienzo = x.index(1)
	for valor in grouper(2,x[comienzo:-1]):
		if valor[0] == valor[1] == 2:
			c.append(3)
		else:
			c.append(\
				0 if (valor[0]==valor[1]) else 1
			)
	#c.reverse()
	print str(c).replace(',','').replace(' ','').replace('3','\n')[1:-1]
		
x_a = [0,]
for i in x:
	x_a.append(x_a[-1] + i)
x_a.pop()

pl.step(x_a,y)
pl.show()
captura.close()
